package com.gitee.sop.layui.adminserver.entrance.filter;

import com.gitee.sop.layui.adminserver.entrance.bo.ServiceContext;
import com.gitee.sop.layui.adminserver.entrance.manager.ServiceContextHolder;
import com.gitee.sop.layui.adminserver.module.base.util.RequestParameterUtil;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 用户访问过滤器，用于记录用户的请求信息
 *
 * @author: huwei
 * @date: 2019/10/17 14:59
 * @version: 1.0.0
 */
@Component
@Order(-70)
public class ServiceContextInitializationFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //业务上下文初始值
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        ServiceContextHolder.setContext(new ServiceContext(RequestParameterUtil.getRequestIp(request), request, response));

        filterChain.doFilter(servletRequest, servletResponse);

        ServiceContextHolder.clear();
    }

    @Override
    public void destroy() {

    }
}
